{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__class__',\n",
       " '__contains__',\n",
       " '__delattr__',\n",
       " '__delitem__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__getitem__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__iter__',\n",
       " '__le__',\n",
       " '__len__',\n",
       " '__lt__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__setattr__',\n",
       " '__setitem__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " 'clear',\n",
       " 'copy',\n",
       " 'fromkeys',\n",
       " 'get',\n",
       " 'items',\n",
       " 'keys',\n",
       " 'pop',\n",
       " 'popitem',\n",
       " 'setdefault',\n",
       " 'update',\n",
       " 'values']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir({})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 常用的方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定义词典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'age': 18, 'name': 'Tony'}\n"
     ]
    }
   ],
   "source": [
    "user_info = {\n",
    "    \"age\": 18,\n",
    "    \"name\": \"Tony\"\n",
    "}\n",
    "print(user_info)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`clear()` 清空字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "user_info = {\n",
    "    \"age\": 18,\n",
    "    \"name\": \"Tony\"\n",
    "}\n",
    "user_info.clear()\n",
    "print(user_info)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`copy()` 复制词典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'age': 18, 'name': 'Tony'}\n"
     ]
    }
   ],
   "source": [
    "user_info = {\n",
    "    \"age\": 18,\n",
    "    \"name\": \"Tony\"\n",
    "}\n",
    "user_info_2 =user_info.copy()\n",
    "print(user_info_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`items()` 返回一个列表, 列表元素类型为元组, 元组的第一个元素是 key, 第二个元素是 value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([('age', 18), ('name', 'Tony')])\n"
     ]
    }
   ],
   "source": [
    "user_info = {\n",
    "    \"age\": 18,\n",
    "    \"name\": \"Tony\"\n",
    "}\n",
    "print(user_info.items())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`keys()` 返回一个列表, 包含字典所有的 key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['age', 'name'])\n"
     ]
    }
   ],
   "source": [
    "user_info = {\n",
    "    \"age\": 18,\n",
    "    \"name\": \"Tony\"\n",
    "}\n",
    "print(user_info.keys())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`values()` 返回一个列表, 包含字典所有的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_values([18, 'Tony'])\n"
     ]
    }
   ],
   "source": [
    "user_info = {\n",
    "    \"age\": 18,\n",
    "    \"name\": \"Tony\"\n",
    "}\n",
    "print(user_info.values())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用 `get` 或者 `[]` 获取字典指定 key 对应的值; 区别在于: 当 key 不存在时, `[]` 会抛出错误, 而 `get` 返回 None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n",
      "18\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "user_info = {\n",
    "    \"age\": 18,\n",
    "    \"name\": \"Tony\"\n",
    "}\n",
    "print(user_info.get(\"age\"))\n",
    "print(user_info[\"age\"])\n",
    "\n",
    "print(user_info.get(\"sex\"))\n",
    "# print(user_info['sex'])\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
